/**
 * ReportGear(2011)
 */
package com.reportgear.designer.editor.object;

import java.awt.BorderLayout;
import java.text.ParseException;
import java.util.Date;

import net.abbp.swing.component.date.JDatePicker;


/**
 * 日期对象编辑
 * 
 * @version 1.0 2011-4-26
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 */
public class DateEditor extends Editor {
	/**
	 * UID
	 */
	private static final long serialVersionUID = -4964246812590059441L;
	private JDatePicker jDatePicker;

	public DateEditor() {
		this(null);
	}

	public DateEditor(boolean isDate) {
		this(null, isDate);
	}

	public DateEditor(boolean isDate, String name) {
		this(null, isDate, name);
	}

	public DateEditor(Object value) {
		this(value, false);
	}

	public DateEditor(Object value, boolean isDate) {
		this(value, isDate, "");
	}

	public DateEditor(Object date, boolean isDate, String name) {
		setLayout(new BorderLayout());
		this.jDatePicker = new JDatePicker();
		if (isDate) {
			this.jDatePicker.setStyle(JDatePicker.STYLE_CN_DATE1);
			this.jDatePicker.setEditable(false);
		}
		this.jDatePicker.setFocusTraversalKeysEnabled(false);
		add(this.jDatePicker, BorderLayout.CENTER);
		setValue(date);
		setName(name);
	}

	public Object getValue() {
		try {
			return this.jDatePicker.getSelectedDate();
		} catch (ParseException ex) {
			ex.printStackTrace();
		}
		return new Date();
	}

	public void setValue(Object value) {
		if ((value == null) || (!(value instanceof Date)))
			value = new Date();
		try {
			this.jDatePicker.setSelectedDate((Date) value);
		} catch (ParseException ex) {
			ex.printStackTrace();
		}
	}

	public void setEnabled(boolean enabled) {
		super.setEnabled(enabled);
		this.jDatePicker.setEnabled(enabled);
	}

	public void requestFocus() {
		this.jDatePicker.requestFocus();
	}

	public boolean accept(Object value) {
		return (value instanceof Date);
	}
}